package edu.drexel.cs575.taskManager.services.user;

import java.sql.Connection;  
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;  
import java.sql.ResultSet;
import java.sql.SQLException;  
import java.sql.Statement;




public class user_api {

	
		public Connection connect(){
			
			 Connection con = null; 
			try {  
			      Class.forName("com.mysql.jdbc.Driver").newInstance();  
			       
			      con = DriverManager.getConnection("jdbc:mysql://localhost:3306/users",  
			        "root", "notasecret");  //   error is here **  error is here ***  error is here   
			  
			     	      
			      } catch(SQLException e) {  
			        
			        e.printStackTrace();  
			    }  
			    catch(Exception e) {  
			      System.err.println("Exception: " + e.getMessage());  
			    }   
			     
			return con;  
			
			
		}
	
	public void connectclose(Connection con){
		try {
			con.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
	}

	
	public boolean adduser (String Authstring, String passhash, String Appname)
	{
		Statement stmt;
		String Appid = returnAppid(Appname);
		
		try {
		if (Appid.equals("")){
			Connection con = connect();
			String insert = "insert into Application (AppName) VALUE ('"+Appname+"')";
			stmt = con.createStatement();
	   		stmt.executeUpdate(insert);
	   		connectclose(con);
	   		Appid = returnAppid(Appname);
			
		}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		try{
		Connection con = connect();
		String insert2 = "insert into Users values ("+Appid+",NULL,'"+Authstring+"','"+passhash+"')";
		System.out.println(insert2);
		stmt = con.createStatement();
   		stmt.executeUpdate(insert2);
   		connectclose(con);
   		return true;
		}
		catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
		return false;
		}

		
	}
	

	public boolean removeuser (String Authstring, String Appname){
		Statement stmt;
		String Appid = returnAppid(Appname);
		
		try{
			Connection con = connect();
			String insert2 = "DELETE from Users where Appid_fk="+Appid+" AND AuthString='"+Authstring+"'";
			stmt = con.createStatement();
	   		int rslt = stmt.executeUpdate(insert2);
	   		connectclose(con);
	   		
	   		if (rslt == 1)
		   	return true;
		   	else
		   	return false;
	   		
			}
		catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return false;
			}
	}
	
	public boolean AuthUser (String Authstring, String passhash, String Appname){
		Statement stmt;
		String Appid = returnAppid(Appname);
		ResultSet rs;
		try{
			Connection con = connect();
			String getauth = "Select Nameid from Users where Appid_fk="+Appid+" AND Authstring='"+Authstring+"' AND Passhash='"+passhash+"'";
			stmt = con.createStatement();
	   		rs = stmt.executeQuery(getauth);
	   							
	   		String Nameid="";
			while (rs.next()) {
				  Nameid = rs.getString("Nameid");
				}
	   		System.out.println("Name:"+Nameid);
			connectclose(con);
			if (!Nameid.equals("")){
				return true;
			}else
				return false;
	   		
	   		
			}
			catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
			}
		
	}
	
	private String returnAppid (String Appname){
		
		Connection con = connect();
		String select = "Select Appid from Application where AppName ='"+Appname+"'";
		String Appid="";
		
		try {
			
		Statement stmt = con.createStatement();
		ResultSet rs;
		rs = stmt.executeQuery(select);
				
		while (rs.next()) {
			  Appid = rs.getString("Appid");
			}
		
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		connectclose(con);
		return Appid;
		
	}


}